package com.nwork.cateye.baidumap;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;

import java.io.IOException;
import java.util.List;

/**
 * Created by LGH on 2015/3/24.
 */
public class MyLocation implements BDLocationListener {
    private LocationClient client;
    private LocationClientOption locationClientOption;
    private LatLng latLng;
    private Context context;
    private final Geocoder geocoder;
    private String address;

    public String getAddress () {
        return address;
    }

    public void setAddress (String address) {
        this.address = address;
    }

    /**
     * 设置初始化定位位置
     */
    public MyLocation (Context context) {
        this.context = context;
        // TODO 需要定位
        locationClientOption = new LocationClientOption();
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        locationClientOption.setOpenGps(true);
        locationClientOption.setNeedDeviceDirect(true);
        locationClientOption.setIsNeedAddress(true);
        locationClientOption.setAddrType("all");
        locationClientOption.setCoorType("bd09ll");
        client = new LocationClient(context, locationClientOption);
        client.registerLocationListener(this);
        client.start();
        geocoder = new Geocoder(context);
    }

    /**
     * 重新定位
     */
    public void reLocation () {
        if (client != null && client.isStarted()) {
            client.requestLocation();
        }
    }

    @Override
    public void onReceiveLocation (BDLocation bdLocation) {
        int locType = bdLocation.getLocType();
        switch (locType) {
            case BDLocation.TypeGpsLocation:
            case BDLocation.TypeNetWorkLocation:
            case BDLocation.TypeCacheLocation:
                String addrStr = bdLocation.getAddrStr();
                String province = bdLocation.getProvince();
                String city = bdLocation.getCity();
                String district = bdLocation.getDistrict();
                String street = bdLocation.getStreet();
                double latitude = bdLocation.getLatitude();
                double longitude = bdLocation.getLongitude();
                Log.d("++++", "  ---addrStr-->>  " + addrStr);
                Log.d("++++", "  ---province-->>  " + province);
                Log.d("++++", "  ---city-->>  " + city);
                Log.d("++++", "  ---district-->>  " + district);
                Log.d("++++", "  ---street-->>  " + street);
                Log.d("++++", "  ---本地-->>  " + latitude);
                Log.d("++++", "  ---本地-->>  " + longitude);
                setAddress(province+city+district+"--"+street);
                break;
        }
    }

    public LatLng nameToLatLng (String name) {

        try {
            List<Address> locationName = geocoder.getFromLocationName(name, 1);
            double latitude = locationName.get(0).getLatitude();
            double longitude = locationName.get(0).getLongitude();
            Log.d("++++", "  ---latitude-->>  " + latitude);
            Log.d("++++", "  ---longitude-->>  " + longitude);
            latLng = new LatLng(latitude, longitude);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return latLng;
    }
}
